home *** CD-ROM | disk | FTP | other *** search
/ Java Interactive Reference Guide / Java Interactive Reference Guide.iso / autorun / source.dir / 00064_15.txt < prev    next >
Encoding:
Text File  |  1980-01-11  |  2.8 KB  |  118 lines

  1. package nathanw;
  2. import nathanw.*;
  3.  
  4. public class Grid {
  5.     public int grid[][];
  6.     public int def; // Default value
  7.     protected int x,y;
  8.     
  9.     public Grid(int sx,int sy,int sinit)
  10.         {
  11.         x=sx;
  12.         y=sy;
  13.         def=sinit;
  14.         grid = new int[x][y];
  15.         for(int i=0;i<x;i++)
  16.              for(int j=0;j<y;j++)
  17.                  grid[i][j]=def;
  18.                 }
  19.  
  20.        public Grid(int sx,int sy)
  21.         {
  22.         this(sx,sy,0);
  23.         }
  24.  
  25.        public Grid(Grid g)
  26.            {
  27.            x=g.x;
  28.            y=g.y;
  29.        grid = new int[x][y];
  30.        for(int i=0;i<x;i++)
  31.            for(int j=0;j<y;j++)
  32.            grid[i][j]=g.grid[i][j];
  33.            }
  34.  
  35.     public void fill(int sx,int sy,int w,int h,int value)
  36.         {
  37.     for(int i=0;i<w;i++)
  38.         for(int j=0;j<h;j++)
  39.                 if((sx+i<x) && (sy+j<y) && (sx+i>=0) && (sy+j>=0))
  40.                     {
  41.             grid[sx+i][sy+j]=value;
  42.             }
  43.            }
  44.  
  45.     public void rotate_cw() // Clockwise 
  46.     {
  47.         int newGrid[][] = new int[y][x];
  48.             int s=y-1;
  49.         for(int i=0;i<y;i++)
  50.         for(int j=0;j<x;j++)
  51.             {
  52.                     newGrid[i  ][j  ]=grid[j][s-i];
  53.                     }
  54.             grid=newGrid;
  55.         int tmp=x;
  56.             x=y;
  57.             y=tmp; 
  58.         }
  59.  
  60.     public void rotate_ccw() // Counterclockwise
  61.     {
  62.         int newGrid[][] = new int[y][x];
  63.             int s=x-1;
  64.         for(int i=0;i<y;i++)
  65.         for(int j=0;j<x;j++)
  66.             newGrid[i  ][j  ]=grid[s-j][i];
  67.             grid=newGrid;
  68.         int tmp=x;
  69.             x=y;
  70.             y=tmp; 
  71.         }
  72.  
  73.        public int sizex() {return x;}
  74.        public int sizey() {return y;}
  75.  
  76.        public void put_on(Grid model,int sx,int sy,intOp map)
  77.        {
  78.            int mx,my;
  79.  
  80.        mx=model.x;
  81.        my=model.y;
  82.        if(mx+sx>x) mx=x-sx;
  83.        if(my+sy>y) my=y-sy;
  84.  
  85.        for(int i=0;i<mx;i++)
  86.         for(int j=0;j<my;j++)
  87.                     if((sx+i<x) && (sy+j<y) &&  (sx+i>=0) && (sy+j>=0))
  88.                     grid[sx+i][sy+j]=map.op(grid[sx+i][sy+j],
  89.                         model.grid[i][j]);
  90.            }
  91.  
  92.     public boolean compare(Grid model,int sx,int sy,intComp comp)
  93.         {
  94.             int mx,my;
  95.  
  96.             mx=model.x;
  97.             my=model.y;
  98.     
  99.         for(int i=0;i<mx;i++)
  100.         for(int j=0;j<my;j++)
  101.             {
  102.                     if((sx+i<x) && (sy+j<y) &&  (sx+i>=0) && (sy+j>=0))
  103.                 {
  104.                         if(comp.comp(grid[sx+i][sy+j],model.grid[i][j]))
  105.                 return true;
  106.                         }
  107.                     else
  108.                         { 
  109.                         if(comp.comp(def,model.grid[i][j]))
  110.                             return true;
  111.                         }
  112.                     } 
  113.         return false;
  114.         }
  115. }
  116.     
  117.     
  118.